package com.ufox.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.Card;
import com.ufox.entity.Result;
import com.ufox.entity.Type;
import com.ufox.service.ICardService;
import com.ufox.service.ICitiesService;
import com.ufox.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private ICardService cardService;

    @GetMapping
    public Result findAll(){
        List<Card> list = cardService.list();
        return Result.ok(list);
    }

    @GetMapping("/{page}/{size}")
    public Result findCardList(@PathVariable int page, @PathVariable int size) {
        Page<Card> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Card> wrapper = new LambdaQueryWrapper<>();
        Page<Card> list = cardService.page(pageInfo, wrapper);
        return Result.ok(list);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Card card = cardService.getById(id);
        return Result.ok(card);
    }

    //添加  或 修改
    @PostMapping("/addOrUpdateCard")
    public Result addOrUpdateCard(@RequestBody Card card) {
        boolean b = cardService.saveOrUpdate(card);
        return  (b ? Result.ok() : Result.error(""));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delCard(@PathVariable Integer id) {
        boolean b = cardService.removeById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

}

